package com.ygj.documentReceipt.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.utils.EasyUtil;
import com.cjy.common.bean.OpAttributes;
import com.ygj.documentReceipt.service.DocumentReceiptService;

/**
 * aproval action
 * 
 * @author Kat Colin
 *
 */

@Action("/documentReceipt")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class DocumentReceiptAction extends AbstractBaseAction{

	private static final long serialVersionUID = 3596625283963784007L;

	private static final Logger log = LoggerFactory
			.getLogger(DocumentReceiptAction.class);

	@Resource
	private DocumentReceiptService documentReceiptService;
	
	private String username;
	private String phone;
	private String fno;
	private int bid;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getFno() {
		return fno;
	}

	public void setFno(String fno) {
		this.fno = fno;
	}

	public int getBid() {
		return bid;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}

	@Action("/authentication")
	public String authentication() {
		try {
			
			if(bid == 4){
				actionResult = new ActionResult(new OpAttributes().add(
						"code",
						documentReceiptService.authentication(username,
								phone)));
			} else {
				actionResult = new ActionResult(new OpAttributes().add(
						"code",
						documentReceiptService.authentication(username,
								EasyUtil.rc4(phone, "szcjy"))));
			}
			
		} catch (Exception e) {
			log.error("aproval authentication exception", e);
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		
		return returnType();
	}

	@Action("/queryOfficialDocumentSignCounts")
	public String queryOfficialDocumentSignCounts() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", 1).add(
					"result",
					new OpAttributes().add("totalCounts", documentReceiptService
							.queryOfficialDocumentSignCounts(username.trim()))));
		} catch (Exception e) {
			log.error("queryAprovals exception", e);
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	@Action("/queryOfficialDocumentSign")
	public String queryOfficialDocumentSign() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", 1).add("result",
					documentReceiptService.queryOfficialDocumentSign(username)));
		} catch (Exception e) {
			log.error("queryOfficialDocumentSign exception", e);
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}
	
	@Action("/queryOfficialDocumentSignDetail")
	public String queryOfficialDocumentSignDetail() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", 1).add("result", documentReceiptService.queryOfficialDocumentSignDetail(fno)));
		} catch (Exception e) {
			log.error("queryOfficialDocumentSignDetail exception", e);
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		
		return returnType();
	}
	
	@Action("/officialDocumentSign")
	public String sign() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", documentReceiptService.sign(username, fno)));
		} catch (Exception e) {
			log.error("queryOfficialDocumentSignDetail exception", e);
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		
		return returnType();
	}
	
	@Action("/signAll")
	public String signAll() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", documentReceiptService.signAll(username)));
		} catch (Exception e) {
			log.error("queryOfficialDocumentSignDetail exception", e);
			e.printStackTrace();
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		
		return returnType();
	} 
}
